package com.briup.smartlabs.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.briup.smartlabs.bean.ex.DeviceType;
import com.briup.smartlabs.common.exceptions.DataValidateException;
import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.common.utils.ResponseCodeEnum;
import com.briup.smartlabs.service.DeviceTypeService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/*
 * 所有和设备类型相关的服务
 */
@Api(tags = "设备类型服务")
@RestController
@RequestMapping(value = "/deviceType")
@RequiredArgsConstructor
public class DeviceTypeController {


	private final DeviceTypeService deviceTypeService;

	//可以先不处理
	/*
		逻辑上比较复杂
	    要求批量删除的时候，返回一个map，标记出来哪些删除成功哪些删除失败
	    {
	   		"success": [1,3,5],
	   		"failure": [2,4,6]
	     }
	 */
	@DeleteMapping("/batch")
	@ApiOperation("删除")
	public Response<Map<String,List<String>>> deleteByBatch(@RequestParam String[] id) {
		Map<String, List<String>> map = deviceTypeService.removeByBatch(id);
		return Response.ok(map);
	}


	@ApiOperation(value = "条件+分页查询", notes = "关键字是设备类型名")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum",value = "指定页码",required = true, defaultValue = "1",dataType = "int"),
			@ApiImplicitParam(name = "pageSize",value = "每页数据条数", allowableValues = "5,10,20,50",defaultValue = "5", dataType = "int",required = true),
			@ApiImplicitParam(name = "name",value = "类型名",required = false,dataType = "string")
			})
	@GetMapping(value = "/page/by/condition")
	public Response<PageInfo<DeviceType>> findByCondition(int pageSize, int pageNum, String name){

		// 调用service层代码
		PageInfo<DeviceType> deviceTypeByPage = deviceTypeService.findByCondition(pageSize, pageNum, name);

		return Response.ok(deviceTypeByPage);
	}


	@DeleteMapping("{id}")
	@ApiOperation("根据id删除")
	public Response<String> deleteById(@PathVariable("id") String id) {
		// 调用service层的代码
		// 希望能够通过异常捕获返回不同的结果
		try {
			deviceTypeService.removeById(id);
		}catch (RuntimeException e){
			return Response.error(ResponseCodeEnum.DATA_NOT_FOUND);
		}
		return Response.ok("删除设备成功！");
	}

	@PostMapping("/saveOrUpdate")
	@ApiOperation("更新和添加")
	public Response<String> saveOrUpdate(@RequestBody DeviceType deviceType) {
		deviceTypeService.saveOrUpdate(deviceType);
		return Response.ok("操作成功");
	}


}








